package com.sunjs.wechat.service.impl;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sunjs.model.LyProductSku;
import com.sunjs.model.LyShoppingCart;
import com.sunjs.wechat.dao.WxShoppingCartMapper;
import com.sunjs.wechat.service.WxShoppingCartService;

import net.sf.json.JSONObject;

@Service
public class WxShoppingCartServiceImpl implements WxShoppingCartService {

	@Autowired
	private WxShoppingCartMapper wxShoppingCartMapper;

	@Override
	public JSONObject insert(LyShoppingCart lyShoppingCart) {
		 // 結果
        JSONObject result = new JSONObject();
        //根据skuid查询库存
        LyProductSku sku = wxShoppingCartMapper.selectSkuBySkuId(lyShoppingCart);
        if(sku == null) {
            result.put("code", "1");
            result.put("msg", "此商品库存不足");
            return result;
        }
        LyShoppingCart car = wxShoppingCartMapper.selectShoppingCart(lyShoppingCart);
        
        if(car == null) {
        	int one = wxShoppingCartMapper.insert(lyShoppingCart);
        	if(one<1) {
        		result.put("code", "1");
                result.put("msg", "系统错误");
                return result;
        	}
        }else {
        	lyShoppingCart.setTotal(Integer.valueOf(lyShoppingCart.getTotal()) + Integer.valueOf(car.getTotal()));
        	int two = wxShoppingCartMapper.update(lyShoppingCart);
        	if(two<1) {
        		result.put("code", "1");
                result.put("msg", "系统错误");
                return result;
        	}
        }
        
        result.put("code", "0");
        result.put("msg", "检查通过");
        return result;
	}

	@Override
	public JSONObject update(LyShoppingCart lyShoppingCart) {
		 // 結果
        JSONObject result = new JSONObject();
        
        //根据skuid查询库存
        LyProductSku sku = wxShoppingCartMapper.selectSkuBySkuId(lyShoppingCart);
        if(sku == null) {
            result.put("code", "1");
            result.put("msg", "此商品库存不足");
            return result;
        }
        int one= wxShoppingCartMapper.update(lyShoppingCart);
        if(one<1) {
    		result.put("code", "1");
            result.put("msg", "系统错误");
            return result;
    	}
        result.put("code", "0");
        result.put("msg", "检查通过");
        return result;
	}

	@Override
	public int delete(LyShoppingCart lyShoppingCart) {
		return wxShoppingCartMapper.delete(lyShoppingCart);
	}

	@Override
	public List<LyShoppingCart> select(LyShoppingCart lyShoppingCart) {
		return wxShoppingCartMapper.select(lyShoppingCart);
	}

	
}
